package com.xinyu126.location.trace

import android.location.Location
import com.magical.location.client.ILocationClient
import com.magical.location.client.LocationListener
import com.magical.location.model.LocationServiceState
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow

interface UIBindings {
    fun states(): StateFlow<Boolean>
    fun stop()
    fun start()
}

class StartUIModel(var client: ILocationClient):LocationListener, UIBindings{
    private var started = MutableStateFlow(false)
    override fun states(): StateFlow<Boolean> {
        return started.asStateFlow()
    }

    override fun start() {
        client.start()
    }

    override fun stop() {
        client.stop()
    }

    override fun onGnssStatusChanged(count: Int, signal: Int, label: String) {
    }

    override fun onProviderStatusChanged(
        provider: String,
        enable: Boolean,
        isLocationEnabled: Boolean
    ) {
    }

    override fun onLocationChanged(location: Location) {
    }

    override fun onLocationServiceStateChanged(state: LocationServiceState) {
        when (state) {
            LocationServiceState.Connecting -> {
                started.value = false
            }

            LocationServiceState.Connected -> {
                started.value = true
            }

            LocationServiceState.Disconnected -> {
                started.value = false
            }
        }
    }

    override fun onLocationError(message: String, throwable: Throwable?) {
        super.onLocationError(message, throwable)
    }
}